﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Order_Management
{
    class OrderDetails
    {
        public OrderDetails(Customer customer)
        {
            this.customer = customer;
            this.goodsList = new List<Goods>();
        }

        private Customer customer;
        private int orderAmount = 0;
        public List<Goods> goodsList;

        public string GetCustomer()
        {
            return this.customer.customerName;
        }
        public void SetCustomer(string name)
        {
            this.customer.customerName = name;
        }

        public int amount
        {
            get
            {
                return this.orderAmount;
            }
            set
            {
                this.orderAmount = value;
            }
        }

        public void AddGoods(Goods goods)
        {
            this.goodsList.Add(goods);
            this.orderAmount = this.orderAmount + (goods.number * goods.price);
        }

        public void DeleteGoods(string name)
        {
            bool isFind = false;
            int index = 0;
            foreach (Goods n in this.goodsList)
            {
                if (name == n.name)
                {
                    isFind = true;
                    index = this.goodsList.IndexOf(n);
                }
            }
            if (isFind)
            {
                this.amount = this.amount - (this.goodsList[index].price - this.goodsList[index].number);
                this.goodsList.RemoveAt(index);
            }
            else
            {
                Console.WriteLine("未找到该商品");
            }
        }

        public void UpdateAmount()
        {
            this.amount = 0;
            foreach (Goods n in this.goodsList)
            {
                this.amount += (n.number * n.price);
            }
        }

        public override string ToString()
        {
            string detail = "客户为：" + this.customer.ToString() + " "
                          + "金额为：" + Convert.ToString(this.amount) + "\n ";
            foreach (Goods n in this.goodsList)
            {
                detail += "第" + this.goodsList.IndexOf(n) + "种商品为：\n";
                detail += n.ToString();
            }
            return detail;
        }

        public override bool Equals(object obj)
        {
            return obj is OrderDetails details &&
                   EqualityComparer<Customer>.Default.Equals(customer, details.customer) &&
                   orderAmount == details.orderAmount &&
                   EqualityComparer<List<Goods>>.Default.Equals(goodsList, details.goodsList) &&
                   amount == details.amount;
        }

        public override int GetHashCode()
        {
            int hashCode = 197039906;
            hashCode = hashCode * -1521134295 + EqualityComparer<Customer>.Default.GetHashCode(customer);
            hashCode = hashCode * -1521134295 + orderAmount.GetHashCode();
            hashCode = hashCode * -1521134295 + EqualityComparer<List<Goods>>.Default.GetHashCode(goodsList);
            hashCode = hashCode * -1521134295 + amount.GetHashCode();
            return hashCode;
        }
    }
}
